﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.SYS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.ServiceModel;
using System.Web.Mvc;
using System.Text;

namespace KPIS.GERP.WCF.GIMS
{
    [ServiceContract]
    public interface IPrePurchaseRequestService
    {
        [OperationContract]
        IEnumerable<PrePurchaseRequest> Select(PrePurchaseRequest criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        int Insert(PrePurchaseRequest model, int createByUserId);

        [OperationContract]
        void Update(PrePurchaseRequest model, int updateByUserId);

        [OperationContract]
        int UpdateAlter(PrePurchaseRequest model, int updateByUserId);

        [OperationContract]
        void Delete(int id, int deleteByUserId);

        [OperationContract]
        int CreateCommitteeGroupIfNotExist(int pprId);

        [OperationContract]
        void UpdateCommiteeGroup(List<int> pprIds, int committeeGroupId);
    }

    public class PrePurchaseRequestService : IPrePurchaseRequestService
    {
        public IEnumerable<PrePurchaseRequest> Select(PrePurchaseRequest criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<PrePurchaseRequest> query = context.PrePurchaseRequests;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Insert(PrePurchaseRequest model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.Code = KPIS.GERP.Models.Class.CodeGenerate.GetNextCode(CodeGenerate.TransectionType.PrePurchaseRequest, "");
                    model.RecordStatus = RecordStatus.WaitStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    //model.Code = CodeGenerate.GetNextCode(CodeGenerate.TransectionType.PrePurchaseRequest);
                    context.PrePurchaseRequests.Add(model);
                    context.SaveChanges();

                    return model.PprId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(PrePurchaseRequest model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int UpdateAlter(PrePurchaseRequest model, int updateByUserId)
        {
            int ret = 0;
            try
            {

                using (var context = new GERPContext())
                {
                    //context.Entry(model).State = EntityState.Modified;
                    //model.UpdatedBy = updateByUserId;
                    //model.UpdatedWhen = DateTime.Now;
                    //context.SaveChanges();

                    var original = context.PrePurchaseRequests.Find(model.PprId);

                    if (original != null)
                    {
                        context.Entry(original).CurrentValues.SetValues(model);
                        ret = context.SaveChanges();
                    }

                }
                return ret;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public void Delete(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.PrePurchaseRequests.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int CreateCommitteeGroupIfNotExist(int pprId)
        {
            using (var context = new GERPContext(true))
            {
                var ppr = context.PrePurchaseRequests.FirstOrDefault(o => o.PprId == pprId && o.RecordStatus != "D");
                CommitteeGroup committeeGroup = null;
                if (ppr != null)
                {
                    if (ppr.CommitteeGroupId != null)
                        return ppr.CommitteeGroupId.Value;
                    else
                    {
                        committeeGroup = context.CommitteeGroups.Add(new CommitteeGroup());
                        committeeGroup.CreateByPprId = pprId;
                        committeeGroup.RecordStatus = "U";
                        context.SaveChanges();
                        ppr.CommitteeGroupId = committeeGroup.CommitteeGroupId;
                        context.SaveChanges();
                    }
                }
                return 0;
            }
        }

        public void UpdateCommiteeGroup(List<int> pprIds, int committeeGroupId)
        {
            using (var context = new GERPContext(true))
            {
                foreach (var ppr in context.PrePurchaseRequests.Where(o => pprIds.Contains(o.PprId)))
                {
                    ppr.CommitteeGroupId = committeeGroupId;
                }
                context.SaveChanges();
            }
        }
    }
}